package com.webull.information.center.carwler.common.util.news.detection;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.NewsDetection;
import com.webull.information.center.carwler.common.util.jsoup.HttpJsoupClient;

@Component
public class DetectionReutersList implements DetectionList {
	private final Logger logger = LogManager.getLogger(getClass());
	private @Autowired
    HttpJsoupClient htmlPrase;

	private static final String R_URL = "http://www.reuters.com/finance/stocks/companyNews?";

	// String url2 =
	// "http://www.reuters.com/finance/stocks/companyNews?symbol=AAPL.O";
	// url2 =
	// "http://www.reuters.com/finance/stocks/companyNews?symbol=600036.SS";
	@Override
	public List<NewsDetection.DetectionResult> parseNewsList(NewsDetection.Detection ticker) {
		String url = R_URL + "symbol=" + ticker.getsSymbol() + "." + ticker.getsExchange();
		Document doc = htmlPrase.parseDocumentFromUrl(url, true);
		if (doc == null) {
			logger.warn("DetectionReutersList parseNewsList parse url:{} ,doc is null!!", url);
			return null;
		}
		return parseNewsList(ticker, doc);
	}

	private List<NewsDetection.DetectionResult> parseNewsList(NewsDetection.Detection ticker, Document doc) {
		List<NewsDetection.DetectionResult> result = new ArrayList<>();

		final Consumer<Element> operation = it0 -> {
			NewsDetection.DetectionResult item = new NewsDetection.DetectionResult(ticker);
			Optional.ofNullable(it0.select("h2>a").first()).ifPresent(a0 -> {
				item.setUrl(StringUtils.stripToNull(a0.absUrl("href")));
				item.setUrlMd5(HttpJsoupClient.finishingMd5Url(item.getUrl() + item.getsSource()));
				item.setTitle(StringUtils.stripToNull(a0.html()));
				item.setNewsTime(DateUtils.addHours(new Date(), -1));// 默认发布一小时了，粗略统计
			});

			if (StringUtils.isNotBlank(item.getUrl()) && StringUtils.isNotBlank(item.getTitle()) //
					&& !StringUtils.startsWithIgnoreCase(item.getTitle(), "UPDATE "))// update
																						// 标题的新闻忽略
			{
				result.add(item);
			}
		};

		// 美股新闻页面
		Optional.ofNullable(doc.select("div.module div.moduleBody div.topStory").first()).ifPresent(topStory -> {
			operation.accept(topStory);
		});

		Optional.ofNullable(doc.select("div.module div.moduleBody div.feature")).ifPresent(features -> {
			for (Element feature : features) {
				operation.accept(feature);
			}
		});
		return result;
	}

}
